package kr.hkit.androidcomplete03.ch25;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import kr.hkit.androidcomplete03.R;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

public class FileIO extends Activity {
	EditText mEdit;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fileio);
		
		mEdit = (EditText)findViewById(R.id.edittext);
	}
	
	public void mOnClick(View v){
		switch(v.getId()){
		case R.id.save:
			try {
				FileOutputStream fos = openFileOutput("test.txt", Context.MODE_PRIVATE);
				String str = "android file io test";
				fos.write(str.getBytes());
				fos.close();
				mEdit.setText("write success");
			} catch (Exception e) {;}
			break;
		case R.id.load:
			FileInputStream fis;
			try {
				fis = openFileInput("test.txt");
				byte[] data = new byte[fis.available()];
				while(fis.read(data) != -1){;}
				fis.close();
				mEdit.setText(new String(data));
			} catch (FileNotFoundException e) {
				mEdit.setText("file not found");
			} catch (Exception e) {;}
			break;
			
		}
	}
}
